package com.heima.shopuploadapi.controller;

import com.heima.shopuploadapi.utils.AliOSSUtil;
import com.zijie.core.util.JsonResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {

    //MultipartFile封装图片上传的所有信息

    @RequestMapping("/upload")
    public ResponseEntity<JsonResult<?>> upload(@RequestParam("file") MultipartFile file){
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //1.png
        String filename = file.getOriginalFilename();
        String extend = filename.substring(filename.lastIndexOf("."));
        String newFileName=uuid+extend;
        String url= "";
        try {
            url = AliOSSUtil.uploadFile(newFileName, file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResponseEntity.ok(JsonResult.success(url));
    }


}
